package com.hqyj.hrms.modules.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.hrms.modules.attendance.dao.AttendanceMapper;
import com.hqyj.hrms.modules.attendance.entity.Attendance;
import com.hqyj.hrms.modules.attendance.service.AttendanceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.Record;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-06
 */
@Service
public class AttendanceServiceImpl  implements AttendanceService {
    @Autowired(required = false)
    private AttendanceMapper attendanceMapper;

    @Override
    public HashMap<String, Object> add(Attendance attendance) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = attendanceMapper.insert(attendance);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Attendance attendance) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = attendanceMapper.updateById(attendance);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer attId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = attendanceMapper.deleteById(attId);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> list(Attendance attendance) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String keyWord = attendance.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Record>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "att_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "month", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "date", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_one", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_two", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_three", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_four", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_five", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "type_six", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "notes", keyWord);
        Page<Attendance> attendancePage = new Page<>(attendance.getPage(),attendance.getRow());
//        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(
//                attendance.getEmpId()!=null&&!attendance.getEmpId().equals(""),
//                "emp_id",
//                attendance.getEmpId());

        Page<Attendance> pageList = attendanceMapper.selectPage(attendancePage,wrapper);
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;

    }

    @Override
    public HashMap<String, Object> selectByMonth(String month) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String,Object>params = new HashMap<>();
        params.put("month",month);
        QueryWrapper<Attendance>wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Attendance> attendancePage = new Page<>(1,5);
        Page<Attendance> pageList = attendanceMapper.selectPage(attendancePage,wrapper);
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;

    }
}
